﻿using App.Common;
using App.IService;
using App.Model;
using App.Model.DTO.Article;
using App.Model.DTO.Role;
using App.Model.DTO.User;
using App.Service;
using App.WebAPI.Utilty.ApiResult;
using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System.Linq.Expressions;
using System.Security.Claims;

namespace App.WebAPI.Controllers
{
    [Route("[controller]/[Action]")]
    [ApiController]
    [Authorize]
    public class RoleController : ControllerBase
    {
        private readonly ILogger<RoleController> _logger;
        private readonly IRoleService _iRoleService;
        public RoleController(IRoleService iRoleService, ILogger<RoleController> logger)
        {
            _iRoleService = iRoleService;
            _logger = logger;
        }
        /// <summary>
        /// 获取角色列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public async Task<ApiResult> GetList()
        {
            var role = await _iRoleService.QueryAsync();
            if (role.Count == 0) return ApiResultHelper.Error("没有更多角色");
            return ApiResultHelper.Success(role,true);
        }
        /// <summary>
        /// 根据角色Id查询
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> GetById(string Id)
        {
            var roleInfo = await _iRoleService.FindAsync(Id);

            return ApiResultHelper.Success(roleInfo,true);
        }

        /// <summary>
        /// 角色分页查询
        /// </summary>
        /// <param name="iMapper"></param>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> GetPageList([FromBody] PageRequest req, [FromServices] IMapper iMapper)
        {
            RefAsync<int> total = 0;
            Expression<Func<Role, bool>> func = u => true;
            List<Role> role = null;
            if (!string.IsNullOrEmpty(req.Filter))
            {
                func = u => u.Name.Contains(req.Filter) || u.Description.Contains(req.Filter);
            }
            if (req.PageIndex == 1 && req.PageSize == int.MaxValue)
            {
                var allRoles = func != null ? await _iRoleService.QueryAsync(func) : await _iRoleService.QueryAsync();
                total = allRoles.Count;
                role = allRoles;
            }
            else
            {
                role = await _iRoleService.QueryAsync(func ?? (u => true), req.PageIndex, req.PageSize, total);
            }
            try
            {
                var roleDTO = iMapper.Map<List<Role>>(role);
                return ApiResultHelper.Success(roleDTO, total,true);
            }
            catch (Exception)
            {
                return ApiResultHelper.Error("服务器异常");
            }
        }
        /// <summary>
        /// 创建角色
        /// </summary>
        /// <param name="name"></param>
        /// <param name="description"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> Create(CreateRoleRequestInput req)
        {
            var id = GuidCombGenerator.GenerateCombGuid32();
            #region 数据验证
            if (String.IsNullOrWhiteSpace(req.Name)) return ApiResultHelper.Error("分类名称不能为空");
            #endregion
            Role role = new Role
            {
                Id = id,
                Name = req.Name,
                Description = req.Description,
                RoleType = req.RoleType,
                CreateAt = DateTime.Now,
                UpdateAt = DateTime.Now,
            };
            bool b = await _iRoleService.CreateAsync(role);
            if (!b) return ApiResultHelper.Error("添加失败，服务器异常");
            return ApiResultHelper.Success(role, b);
        }
        /// <summary>
        /// 编辑角色
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> Edit(UpdateRoleRequestInput req)
        {
            if (string.IsNullOrEmpty(req.Id))
            {
                return ApiResultHelper.Error("参数不能为空");
            };

            var role = await _iRoleService.FindAsync(req.Id);
            role.Name = req.Name;
            role.Description = req.Description;
            role.UpdateAt = DateTime.Now;
            bool b = await _iRoleService.EditAsync(role);
            if (!b) return ApiResultHelper.Error("修改失败，服务器异常");
            return ApiResultHelper.Success("修改成功",b);
        }
        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> Delete(DeleteRoleRequestInput req)
        {
            bool b = await _iRoleService.DeleteAsync(req.Id);
            if (!b) return ApiResultHelper.Error("删除失败，服务器异常");
            return ApiResultHelper.Success(null,b);
        }
    }
}
